/**
 * 
 * purpose: button controller, button is a MovieClip...
 * @auth: Ning
 * date: 2009-9-16
 * 
 * */
package com.components.button
{
	import core.ease.MotionController;
	import core.section.Controller;
	
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[Event(name="click", type="flash.events.MouseEvent")]
    [Event(name="mouse_over", type="flash.events.MouseEvent")]
    [Event(name="mouse_out", type="flash.events.MouseEvent")]
    [Event(name="mouse_down", type="flash.events.MouseEvent")]
    [Event(name="mouse_up", type="flash.events.MouseEvent")]
    [Event(name="selected", type= "flash.events.Event")]
	
		
	public class MCButton extends Controller
	{
		//START...
		
		//PROPERTY...
		private var tar:MovieClip;
		
		private var _state:String;
		
		private var _clickToSelected:Boolean;
		
		private var _selected:Boolean;
		
		private var _data:Object;
		
		private var buttonMode:Boolean;
		
		public static const NORMAL_STATE:String = "normal";
		
		public static const OVER_STATE:String = "over";
		
		public static const OUT_STATE:String = "out";
		
		public static const DOWN_STATE:String = "down";
		
		public static const UP_STATE:String = "up";	
		
		public static const SELECTED_STATE:String = "selected";			
			
		//CONSTRUCTOR...
		public function MCButton(target:MovieClip,buttonMode:Boolean = true)
		{
					
			tar = target;
			
			this.buttonMode = buttonMode;
			
			super(target);
			
			init();
									
		}
		
		//METHODS...
		
		//init...
		private function init():void
		{
			
			var labels:Array = tar.currentLabels;
			
			labels.sortOn("frame",Array.NUMERIC);
			
			for(var i:int = 0;i < labels.length;i++)
			{
				
				var label:FrameLabel = labels[i];
				
				tar.addFrameScript((label.frame - 1 < 1 ? tar.totalFrames : label.frame - 1)-1,tar.stop);
				
			}	
			
			tar.buttonMode = buttonMode;
			
			tar.mouseChildren = false;
			
			configTargetListeners();
			
			state = NORMAL_STATE;						
			
		}
		
		//config target event listeners...		
		private function configTargetListeners():void
		{
			
			tar.addEventListener(MouseEvent.MOUSE_OVER,tarOverHandler);
			
			tar.addEventListener(MouseEvent.MOUSE_OUT,tarOutHandler);			
			
			tar.addEventListener(MouseEvent.MOUSE_DOWN,tarDownHandler);
			
			tar.addEventListener(MouseEvent.MOUSE_UP,tarUpHandler);
			
			tar.addEventListener(MouseEvent.CLICK,clickHandler);
						
		}
		
		//target handlers...
		
		//click to selected handler...
		private function clickHandler(evt:MouseEvent):void
		{
			
			if(clickSelected)
			{
				
				selected = true;
				
			}
				
			
			dispatchTargetEvent(evt);	
						
		}
		
		//over handler...
		private function tarOverHandler(evt:MouseEvent):void
		{
			
			if(selected){return};
			
			state = OVER_STATE;
			
			dispatchTargetEvent(evt);
						
		}
		
		//out handler...
		private function tarOutHandler(evt:MouseEvent):void
		{
			
			if(selected){return};
			
			state = OUT_STATE;
			
			dispatchTargetEvent(evt);
			
		}
		
		//up handler...
		private function tarUpHandler(evt:MouseEvent):void
		{
			
			if(selected){return};
			
			state = UP_STATE;
			
			dispatchTargetEvent(evt);
			
		}
		
		//down handler...
		private function tarDownHandler(evt:MouseEvent):void
		{
			
			if(selected){return};
			
			state = DOWN_STATE;
			
			dispatchTargetEvent(evt);
			
		}
		
		//dispathch target event...
		private function dispatchTargetEvent(evt:Event):void
		{
						
			dispatchEvent(evt);
			
		}
		
		//set state..
		public function set state(_state:String):void
		{
												
			this._state = _state;
						
			tar.gotoAndPlay(_state);
			
		}
		
		//get state...
		public function get state():String
		{
			
			return _state;
			
		}
		
		//set selected...
		public function set selected(sel:Boolean):void
		{
			
			if(_selected == sel) return;
			
			_selected = sel;
			
			tar.mouseEnabled = !sel;
			tar.buttonMode = !sel;
						
			if(sel)
			{
				state = SELECTED_STATE;
				
			}else{
				
				state = NORMAL_STATE;
				
			}
									
		}
		
		//get selected...
		public function get selected():Boolean
		{
			
			return _selected;
			
		}
		
		
		//click selected...
		public function set clickSelected(_clickSelected:Boolean):void
		{
			
			this._clickToSelected = _clickSelected;
									
		}
		
		//get cick selected...
		public function get clickSelected():Boolean
		{
			
			return _clickToSelected;
			
		}
		
		
		//set data...
		public function set data(data:Object):void
		{
			
			_data = data;
					
		}
		
		
		//get data...
		public function get data():Object
		{
			
			return _data;
			
		}
		
		//END
	}
}